package com.zhiyume.controller;

import com.zhiyume.dao.entity.TAdmin;
import com.zhiyume.dao.entity.TRole;
import com.zhiyume.service.UserManagerService;
import com.zhiyume.vo.UserReqVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiaxz
 * @date 2017/12/24 15:18
 * @description
 */
@RestController
@RequestMapping("/admin")
public class UserManagerController {

    @Autowired
    private UserManagerService userManagerService;
    @RequestMapping("/userManager")
    public ModelAndView userInfoList(){
        ModelAndView mv = new ModelAndView("page/uManager/userManager");
        return mv;
    }
    @RequestMapping("/getAllUserInfoList")
    public Map queryAllUserInfoList(UserReqVO vo) {
        Map<String,Object> dataMap = new HashMap();
        List<TAdmin> list = userManagerService.queryAllUserInfoList(vo);
        int count = userManagerService.queryCount(vo);
        dataMap.put("list",list);
        dataMap.put("total",count);
        dataMap.put("page",vo.getPage());
        return dataMap;
    }
    @RequestMapping("/editUserPage")
    public ModelAndView editUser(long id) {
        ModelAndView mv = new ModelAndView("page/uManager/editActor");
        TAdmin admin = userManagerService.queryUserInfoById(id);
        mv.addObject("actor",admin);
        return mv;
    }
    @RequestMapping("/updateUser")
    public Map updateUser(@ModelAttribute("form") TAdmin tAdmin) {
        String result = "success";
        try {
            userManagerService.updateUserById(tAdmin);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }
    @RequestMapping("/addUserHtml")
    public ModelAndView addUser() {
        ModelAndView mv = new ModelAndView("page/uManager/addActor");
        List<TRole> role = userManagerService.queryAllRole();
        mv.addObject("cList",role);
        return mv;
    }
    @RequestMapping("/addUser")
    public Map addUser(TAdmin tAdmin,int roleId){
        return userManagerService.addUserBy(tAdmin,roleId);
    }
    @RequestMapping("/deleteUser")
    public Map addUser(long id ){
        return userManagerService.deleteUserById(id);
    }
}
